package cn.xfdzx.android.apps.shop.presenter;

import cn.xfdzx.android.apps.shop.base.BasePresenter;
import cn.xfdzx.android.apps.shop.bean.CarCountBeen;
import cn.xfdzx.android.apps.shop.bean.CreatePusherBeen;
import cn.xfdzx.android.apps.shop.bean.LogininfoBean;
import cn.xfdzx.android.apps.shop.bean.UpdateNumPriceBeen;
import cn.xfdzx.android.apps.shop.bean.VideoEndBeen;
import cn.xfdzx.android.apps.shop.bean.VideoRoomGoodListBeen;
import cn.xfdzx.android.apps.shop.contract.CarNumContract;
import cn.xfdzx.android.apps.shop.contract.CreatePusherContract;
import cn.xfdzx.android.apps.shop.model.CarNumModel;
import cn.xfdzx.android.apps.shop.model.CreatePusherModel;
import cn.xfdzx.android.apps.shop.net.RxScheduler;
import io.reactivex.functions.Consumer;

/**
 * @author azheng
 * @date 2018/6/4.
 * Description：
 */
public class CreatePusherPresenter extends BasePresenter<CreatePusherContract.View> implements CreatePusherContract.Presenter {

    private CreatePusherContract.Model model;

    public CreatePusherPresenter() {
        model = new CreatePusherModel();
    }


    @Override
    public void createPusher(String live_cover, String title) {
        //View是否绑定 如果没有绑定，就不执行网络请求
        if (!isViewAttached()) {
            return;
        }
        mView.showLoading();
        model.createPusher(live_cover, title)
                .compose(RxScheduler.<CreatePusherBeen>Flo_io_main())
                .as(mView.<CreatePusherBeen>bindAutoDispose())
                .subscribe(new Consumer<CreatePusherBeen>() {
                    @Override
                    public void accept(CreatePusherBeen bean) throws Exception {
                        mView.onCreatePusherSuccess(bean);
                        mView.hideLoading();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.onError(throwable);
                        mView.hideLoading();
                    }
                });
    }

    @Override
    public void videoEnd() {
        //View是否绑定 如果没有绑定，就不执行网络请求
        if (!isViewAttached()) {
            return;
        }
//        mView.showLoading();
        model.videoEnd()
                .compose(RxScheduler.<VideoEndBeen>Flo_io_main())
                .as(mView.<VideoEndBeen>bindAutoDispose())
                .subscribe(new Consumer<VideoEndBeen>() {
                    @Override
                    public void accept(VideoEndBeen bean) throws Exception {
                        mView.onVideoEndSuccess(bean);
//                        mView.hideLoading();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.onError(throwable);
//                        mView.hideLoading();
                    }
                });
    }

    @Override
    public void createImGroup(String id, String type) {
        //View是否绑定 如果没有绑定，就不执行网络请求
        if (!isViewAttached()) {
            return;
        }
        //  mView.showLoading();
        model.createImGroup(id, type)
                .compose(RxScheduler.<LogininfoBean>Flo_io_main())
                .as(mView.<LogininfoBean>bindAutoDispose())
                .subscribe(new Consumer<LogininfoBean>() {
                    @Override
                    public void accept(LogininfoBean bean) throws Exception {
                        mView.onCreateImGroupSuccess(bean);
                        //   mView.hideLoading();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.onError(throwable);
                        //   mView.hideLoading();
                    }
                });
    }

    @Override
    public void getLiveImLogin(String id, String type) {
        //View是否绑定 如果没有绑定，就不执行网络请求
        if (!isViewAttached()) {
            return;
        }
        //  mView.showLoading();
        model.getLiveImLogin(id, type)
                .compose(RxScheduler.<LogininfoBean>Flo_io_main())
                .as(mView.<LogininfoBean>bindAutoDispose())
                .subscribe(new Consumer<LogininfoBean>() {
                    @Override
                    public void accept(LogininfoBean bean) throws Exception {
                        mView.onLiveImLoginSuccess(bean);
                        //   mView.hideLoading();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.onError(throwable);
                        //   mView.hideLoading();
                    }
                });
    }

    @Override
    public void videoRoomGoodList(String live_id) {
        if (!isViewAttached()) {
            return;
        }
        //  mView.showLoading();
        model.videoRoomGoodList(live_id)
                .compose(RxScheduler.<VideoRoomGoodListBeen>Flo_io_main())
                .as(mView.<VideoRoomGoodListBeen>bindAutoDispose())
                .subscribe(new Consumer<VideoRoomGoodListBeen>() {
                    @Override
                    public void accept(VideoRoomGoodListBeen bean) throws Exception {
                        mView.onVideoRoomGoodListSuccess(bean);
                        //   mView.hideLoading();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.onError(throwable);
                        //   mView.hideLoading();
                    }
                });
    }

    @Override
    public void updateNumPrice(String id, String marketprice, String stock, String discountprice) {
        if (!isViewAttached()) {
            return;
        }
        mView.showLoading();
        model.updateNumPrice(id, marketprice, stock, discountprice)
                .compose(RxScheduler.<UpdateNumPriceBeen>Flo_io_main())
                .as(mView.<UpdateNumPriceBeen>bindAutoDispose())
                .subscribe(new Consumer<UpdateNumPriceBeen>() {
                    @Override
                    public void accept(UpdateNumPriceBeen bean) throws Exception {
                        mView.onUpdateNumPriceSuccess(bean);
                        mView.hideLoading();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.onError(throwable);
                        mView.hideLoading();
                    }
                });
    }
}
